package com.cn.wanxi.service;

import com.cn.wanxi.IOModel.NavIOModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.ReadAndWriteIO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavService {


    /**
     * 得到导航数据
     *
     * @return
     */
    public List<NavModel> getNavList() {
//  写入记事本
        writeNote(Path.NAV_PATH.getPath());
//  读取记事本
        String result = new ReadAndWriteIO().readNote(Path.NAV_PATH.getPath());
//   封装list
        return setData(result);
    }

    private List<NavModel> setData(String result) {
        String[] results = result.split(Path.SPLIT.getPath());
        List<NavModel> list = new ArrayList<>();
        String href = results[0];
        href = href.replace("[", "").replace("]", "");
        String title = results[1];
        title = title.replace("[", "").replace("]", "");
        String[] hrefs = href.split(",");
        String[] titles = title.split(",");
//        String[] hrefs = model.getHrefs();
//        String[] titles = model.getTitles();
        for (int i = 0; i < hrefs.length; i++) {
            NavModel navModel = new NavModel();//就是为了封装页面导航数据
            navModel.setHref(hrefs[i]);
            navModel.setTitle(titles[i]);
            list.add(navModel);
        }
        return list;
    }
//    private String readNote(String path, String name) {
//        File file = new File(path, name);
////        InputStream inputStream=new FileInputStream(file);//字节流
//        StringBuffer stringBuffer = new StringBuffer();
//        try {
//            Reader reader = new FileReader(file);
//            int result = 0;
//            while ((result = reader.read()) != -1) {
//                stringBuffer.append((char) result);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    /**
     * 写入记事本
     *
     * @param name 文件名称
     */
    private void writeNote(String name) {
        NavIOModel navIOModel = new NavIOModel();
        String hrefs[] = navIOModel.getHrefs();
        String titles[] = navIOModel.getTitles();
        List<String> list = new ArrayList<>();
        list.add(Arrays.toString(hrefs));
        list.add(Arrays.toString(titles));
        new ReadAndWriteIO().createNote(name, list);

    }
}
